package com.example.guiguiapplicatio.api;

import android.os.NetworkOnMainThreadException;

import androidx.fragment.app.Fragment;

import com.example.guiguiapplicatio.api.Interface.GetRecordCallback;
import com.example.guiguiapplicatio.Response.Record;
import com.example.guiguiapplicatio.util.ResponseBody;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class GetNowServeringRecordApi {
    private final Gson gson=new Gson();
    private String ip= GetCountTotalApi.ip;
    private GetRecordCallback getRecordCallback;
    private Fragment fragment;
    public GetNowServeringRecordApi(Fragment fragment) {
        this.fragment = fragment;
    }
    public void setGetRecordCallback(GetRecordCallback getRecordCallback) {this.getRecordCallback = getRecordCallback;
    }
    public void performGetRecordList() {

        new Thread(() -> {
            String url = "http://"+ip+":9090/signing/getNowServering";
            //  请求头
            Headers headers = new Headers.Builder()
                    .add("Accept", "application/json, text/plain, */*")
//                    .add("appId", "用户所申请的应用ID")
//                    .add("appSecret", "用户所申请的应用密钥")
                    .add("Content-Type", "application/json")
                    .build();
            Request request = new Request.Builder()
                    .url(url)
                    // 将请求头加至请求中
                    .headers(headers)
                    .get()
                    .build();
            try {
                OkHttpClient client = new OkHttpClient();
                client.newCall(request).enqueue(callback);
            } catch (NetworkOnMainThreadException ex) {
                ex.printStackTrace();
            }
        }).start();
    }
    private final Callback callback=new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
            if(getRecordCallback!=null){
                getRecordCallback.onFailure(e.getMessage());
            }
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            try {
                if (response.isSuccessful()) {
                    Type jsonType = new TypeToken<ResponseBody<List<Record>>>() {}.getType();
                    String body = response.body().string();
                    ResponseBody<List<Record>> dataResponseBody = gson.fromJson(body, jsonType);
                    if (getRecordCallback != null) {
                        fragment.getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                getRecordCallback.onSuccess(dataResponseBody);
                            }
                        });
                    }
                } else {
                    if (getRecordCallback != null) {
                        getRecordCallback.onFailure("Response was not successful: " + response.code());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                if (getRecordCallback != null) {
                    getRecordCallback.onFailure(e.getMessage());
                }
            }
        }
    };
}
